package com.wgchao.mdmp.dao;

import com.wgchao.mdmp.common.TimeUtil;
import com.wgchao.mdmp.entity.MdmpXl;
import com.wgchao.mdmp.repository.MdmpXlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName: TwDao
 * @author: wgchao
 * @createTime: 2019/11/3 1:11 PM
 * @Description: 心率
 */
@Service
public class XlDao {
    @Autowired
    private MdmpXlRepository xlRepository;

    public MdmpXl save(MdmpXl xl) throws Exception{
        xl.setCreateTime(new Date());
        xl = xlRepository.save(xl);
        return xl;
    }

    public void delete(Long id) throws Exception{
        Optional<MdmpXl> gxyOptional = xlRepository.findById(id);
        if(gxyOptional.isPresent()){
            xlRepository.delete(gxyOptional.get());
        }else {
            throw new Exception("血压记录不存在");
        }
    }

    public void delete(String idcard, Date upTime) throws Exception{
        MdmpXl gxy = xlRepository.findFirstByIdcardAndUpTime(idcard, upTime);
        if(gxy != null){
            xlRepository.delete(gxy);
        }else {
            throw new Exception("血压记录不存在");
        }
    }

    public List<MdmpXl> searchRecords(String idcard, Date upTimeStrat, Date upTimeEnd) throws Exception{
        Specification<MdmpXl> specification = new Specification<MdmpXl>() {
            @Override
            public Predicate toPredicate(Root<MdmpXl> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                Predicate predicate = criteriaBuilder.equal(root.get("idcard"), idcard);
                predicateList.add(predicate);

                if(upTimeStrat != null){
                    Predicate startp = criteriaBuilder.greaterThan(root.get("measureTime"), upTimeStrat);
                    predicateList.add(startp);
                }
                if(upTimeEnd != null){
                    Predicate endp = criteriaBuilder.lessThan(root.get("measureTime"), upTimeEnd);
                    predicateList.add(endp);
                }

                if(upTimeStrat==null && upTimeEnd==null) {
                    Predicate endp = criteriaBuilder.greaterThan(root.get("measureTime"), TimeUtil.getNextDay(new Date(), -7));
                    predicateList.add(endp);
                }
                Predicate[] predicates = new Predicate[predicateList.size()];
                return criteriaBuilder.and(predicateList.toArray(predicates));
            }
        };
        Sort sort = Sort.by(Sort.Order.asc("measureTime"));
        List<MdmpXl> gxies = xlRepository.findAll(specification, sort);

        return gxies;
    }
}
